package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Description NursingProjectServiceImpl
 * @Author songyu
 * @Date 2024-10-15
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    /**
     * 分页查询护理项目
     *
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //定义缓存的key
        String key = "nursing_project::"+(name==null?"":name)+"_"+(status==null?"":status)+"_"+pageNum+"_"+pageSize;
        //查询缓存数据json字符串
        String json = redisTemplate.opsForValue().get(key);
        //缓存数据为空，执行数据库操作，并写入缓存
        PageResponse<NursingProjectVo> pageResponse = null;
        if (StrUtil.isEmpty(json)) {
            //1.定义PageHelper的页码和每页大小
            PageHelper.startPage(pageNum, pageSize);

            //2.执行分页查询数据返回Page<NursingProjectVo>
            Page<NursingProjectVo> page = nursingProjectMapper.findByCondition(name, status);

            //3.封装给PageResponse<NursingProjectVo>
            pageResponse = new PageResponse<>(page);//里面会自动封装页码，每页大小，总条数，总页数
            pageResponse.setRecords(page.getResult());//封装当前页数据列表

            //写入缓存
            redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(pageResponse),30, TimeUnit.MINUTES);
        }else {
            //不为空，将json转换为对象返回
            pageResponse = JSONUtil.toBean(json,PageResponse.class);
        }
        //4.返回数据
        return pageResponse;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO
     */
    @Override
    public void add(NursingProjectDto nursingProjectDTO) {
        //1.将nursingProjectDTO转换为nursingProject
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);

        //2.调用mapper插入数据库
        nursingProjectMapper.insert(nursingProject);

        //删除缓存
        clearCache("nurSing_project::*");
    }

    private void clearCache(String key){
        //key可能带有通配符
        Set<String> keys = redisTemplate.keys(key);
        //删除缓存
        redisTemplate.delete(keys);
    }

    /**
     * 根据id查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo getById(Long id) {
        return nursingProjectMapper.findById(id);
    }

    /**
     * 修改护理项目
     *
     * @param nursingProjectDTO
     */
    @Override
    public void update(NursingProjectDto nursingProjectDTO) {
        //将nursingProjectDTO 准话为 nursingProject
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);

        //调用mapper修改
        nursingProjectMapper.update(nursingProject);

        //删除缓存
        clearCache("nurSing_project::*");
    }
}
